package com.sg.service.biz.component.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.sg.common.util.RedisUtil;
import com.sg.dto.biz.component.req.*;
import com.sg.dto.biz.component.res.*;
import com.sg.entity.OmsPlatformOperationSeparatedData;
import com.sg.service.base.component.MCustomFields3Service;
import com.sg.service.base.component.MOmsPlatformOperationSeparatedDataService;
import com.sg.common.exception.Assert;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @date ：
 */
@Service
public class OperationsDeploymentServiceImpl
        implements com.wicket.okrcomponent.biz.service.OperationsDeploymentService {

    @Resource
    private RedisUtil redisUtil;
    //@Resource
    //private MMmmShellService mMmmShellService;
    @Resource
    private NbOperationsDeployment nbOperationsDeployment;
    @Resource
    private MCustomFields3Service mCustomFields3Service;
    //@Resource
    //private MSpaceService mSpaceService;
//@Resource
    //private undefinedService undefinedService;
    @Resource
    private MOmsPlatformOperationSeparatedDataService mOmsPlatformOperationSeparatedDataService;

    /**
     * D2执行动态寻表删除数据(公共)[7190]
     * gen by moon at 6/11/2023, 8:53:47 PM
     */
    @Trace(operationName = "D2执行动态寻表删除数据(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ImplementDynamicLookupDeleteDataComRespDto implementDynamicLookupDeleteDataCom(ImplementDynamicLookupDeleteDataComReqDto reqDto) {


        //步骤0: M2批量动态寻表删除数据(特殊方法) - batchDeleteDynamicLookupDeleteData
        //ModelCode: dynamicLookupDeleteData
        BatchDeleteDynamicLookupDeleteDataRespDto dynamicLookupDeleteDataRes = null;
        BatchDeleteDynamicLookupDeleteDataReqDto dynamicLookupDeleteDataReq = new BatchDeleteDynamicLookupDeleteDataReqDto();
        if (reqDto != null) {
            dynamicLookupDeleteDataReq.setToolTableEncryptionName(reqDto.getToolTableEncryptionName());//SimpleFieldAssign//sourceId:1035875_1
            dynamicLookupDeleteDataReq.setEntityName(reqDto.getEntityName());//SimpleFieldAssign//sourceId:1035884_1
            dynamicLookupDeleteDataReq.setDynamicLookupDataList(reqDto.getDynamicLookupDataList());//list-field-assign//sourceId:1035202_1
        }

        /*M2批量动态寻表删除数据(特殊方法)[7191]   */
        Assert.isNull(dynamicLookupDeleteDataReq.getToolTableEncryptionName(), "D2执行动态寻表删除数据(公共)-M2批量动态寻表删除数据(特殊方法)-设计工具加密表名不能为空", false);
        Assert.isNull(dynamicLookupDeleteDataReq.getEntityName(), "D2执行动态寻表删除数据(公共)-M2批量动态寻表删除数据(特殊方法)-内容表主键字段英文名不能为空", false);
        dynamicLookupDeleteDataRes = nbOperationsDeployment.batchDeleteDynamicLookupDeleteData(dynamicLookupDeleteDataReq);


        ImplementDynamicLookupDeleteDataComRespDto retData = new ImplementDynamicLookupDeleteDataComRespDto();


        return retData;
    }

    /**
     * D2执行动态寻表数据(公共)[7186]
     * gen by moon at 6/11/2023, 5:50:41 PM
     */
    @Trace(operationName = "D2执行动态寻表数据(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ImplementDynamicLookupDataComRespDto implementDynamicLookupDataCom(ImplementDynamicLookupDataComReqDto reqDto) {


        QueryDynamicLookupDataComListRespDto dynamic_lookup_dataRes_1 = null;
//步骤0: M2查询动态寻表数据列表(特殊方法) - queryDynamicLookupDataList
        //ModelCode: dynamic_lookup_data
        QueryDynamicLookupDataComListRespDto dynamic_lookup_dataRes = null;
        QueryDynamicLookupDataComListReqDto dynamic_lookup_dataReq = new QueryDynamicLookupDataComListReqDto();
        if (reqDto != null) {
            dynamic_lookup_dataReq.setToolTableEncryptionName(reqDto.getToolTableEncryptionName());//SimpleFieldAssign//sourceId:1032558_1
            dynamic_lookup_dataReq.setEvaluationTemplateId(reqDto.getEvaluationTemplateId());//SimpleFieldAssign//sourceId:1032561_1
            dynamic_lookup_dataReq.setThemeContentId(reqDto.getThemeContentId());//SimpleFieldAssign//sourceId:1032560_1
            dynamic_lookup_dataReq.setCycleDataObjId(reqDto.getCycleDataObjId());//SimpleFieldAssign//sourceId:1032563_1
            dynamic_lookup_dataReq.setSpaceId(reqDto.getSpaceId());//SimpleFieldAssign//sourceId:1032562_1
        }

        /*M2查询动态寻表数据列表(特殊方法)[7187]  用于运维接口，根据设计工具加密表名、空间ID、模版ID，出参表主键ID */
        Assert.isNull(dynamic_lookup_dataReq.getToolTableEncryptionName(), "D2执行动态寻表数据(公共)-M2查询动态寻表数据列表(特殊方法)-设计工具加密表名不能为空", false);
        dynamic_lookup_dataRes = nbOperationsDeployment.queryDynamicLookupDataComList(dynamic_lookup_dataReq);


        dynamic_lookup_dataRes_1 = dynamic_lookup_dataRes;

        ImplementDynamicLookupDataComRespDto retData = new ImplementDynamicLookupDataComRespDto();
        if (dynamic_lookup_dataRes_1 != null) {
            retData.setDynamicLookupDataList(dynamic_lookup_dataRes_1.getDynamicLookupDataList().stream().map(item -> BeanUtil.toBean(item, DynamicLookupDataDto.class)).collect(Collectors.toList()));//objList-to-objLists//sourceId:1032569_1
        }


        return retData;
    }

    /**
     * D2执行私有化分离数据(公共)[7193]
     * gen by moon at 6/12/2023, 11:59:09 PM
     */
    @Trace(operationName = "D2执行私有化分离数据(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ImplementPrivatizationAndSeparationDataComRespDto implementPrivatizationAndSeparationDataCom(ImplementPrivatizationAndSeparationDataComReqDto reqDto) {


        ObtainReceiveFieldsRespDto receptionServiceRes_1 = null;
//virtualUsage M2获取数据集按入参过滤指定记录(特殊方法)  45258
        //ModelCode: dataFilterSpecRecBy
        ObtainDataFilterSpecRecByRespDto dataFilterSpecRecByRes = null;
        ObtainDataFilterSpecRecByReqDto dataFilterSpecRecByReq = new ObtainDataFilterSpecRecByReqDto();
        if (reqDto != null) {
            dataFilterSpecRecByReq.setDataFilterSpecRecByList(reqDto.getSpaceList());//list-field-assign//sourceId:1036557_1_45258
            dataFilterSpecRecByReq.setComTxtField(reqDto.getTenantSpaceId());//SimpleFieldAssign//sourceId:1036558_1_45258
        }

    /*M2获取数据集按入参过滤指定记录(特殊方法)[7205]  目前用于购物车步骤判断。入参：步骤数据集+三个步骤字段（字段名与数据集中步骤编码相同）
处理逻辑：入参步骤字段值不为1，则将数据集中步骤编码同名的记录从出参数据集中删除 */
        Assert.isNull(dataFilterSpecRecByReq.getComTxtField(), "D2执行私有化分离数据(公共)-M2获取数据集按入参过滤指定记录(特殊方法)-通用文本字段不能为空", false);
        dataFilterSpecRecByRes = nbOperationsDeployment.obtainDataFilterSpecRecBy(dataFilterSpecRecByReq);


//virtualUsage M2-获取空间列表  45259
        //ModelCode: receptionService
        ObtainReceiveFieldsRespDto receptionServiceRes = null;
        ObtainReceiveFieldsReqDto receptionServiceReq = new ObtainReceiveFieldsReqDto();


        /*M2-获取空间列表[4007]  用于特殊方法接收上游入参。 */
        Assert.isNull(receptionServiceReq.getTenantSpaceId(), "D2执行私有化分离数据(公共)-M2-获取空间列表-租户空间ID不能为空", false);
        receptionServiceRes = nbOperationsDeployment.obtainReceiveFields(receptionServiceReq);


        receptionServiceRes_1 = receptionServiceRes;
//virtualUsage M2执行空间发牌【循环开始】  45260
        //ModelCode: circulationCollections
        for (String circulationCollectionsRes : receptionServiceRes.getSpaceList()) {

//virtualUsage D2执行动态寻表数据(公共)  45262
            ImplementDynamicLookupDataComRespDto implementDynamicLookupDataComRespDto = null;
            ImplementDynamicLookupDataComReqDto implementDynamicLookupDataComReqDto = new ImplementDynamicLookupDataComReqDto();
            if (reqDto != null) {
                implementDynamicLookupDataComReqDto.setToolTableEncryptionName(reqDto.getToolTableEncryptionName());//SimpleFieldAssign//sourceId:1035886_1_45262
            }
            if (circulationCollectionsRes != null) {
                implementDynamicLookupDataComReqDto.setSpaceId(circulationCollectionsRes);//SimpleFieldAssign//sourceId:1035890_1_45262
            }

            /*D2执行动态寻表数据(公共)[7186]   */
            Assert.isNull(implementDynamicLookupDataComReqDto.getToolTableEncryptionName(), "D2执行私有化分离数据(公共)-D2执行动态寻表数据(公共)-设计工具加密表名不能为空", false);
            Assert.isNull(implementDynamicLookupDataComReqDto.getSpaceId(), "D2执行私有化分离数据(公共)-D2执行动态寻表数据(公共)-创建于空间ID不能为空", false);
            implementDynamicLookupDataComRespDto = implementDynamicLookupDataCom(implementDynamicLookupDataComReqDto)/*vcase invoke 同服务,同domain*/;


//virtualUsage D2执行动态寻表删除数据(公共)  45263
            ImplementDynamicLookupDeleteDataComRespDto implementDynamicLookupDeleteDataComRespDto = null;
            if (implementDynamicLookupDataComRespDto != null) {
                ImplementDynamicLookupDeleteDataComReqDto implementDynamicLookupDeleteDataComReqDto = new ImplementDynamicLookupDeleteDataComReqDto();
                if (reqDto != null) {
                    implementDynamicLookupDeleteDataComReqDto.setToolTableEncryptionName(reqDto.getToolTableEncryptionName());//SimpleFieldAssign//sourceId:1036569_1_45263
                    implementDynamicLookupDeleteDataComReqDto.setEntityName(reqDto.getEntityName());//SimpleFieldAssign//sourceId:1036570_1_45263
                }
                if (implementDynamicLookupDataComRespDto != null && implementDynamicLookupDataComRespDto.getDynamicLookupDataList() != null && !CollectionUtil.isEmpty(implementDynamicLookupDataComRespDto.getDynamicLookupDataList())) {
                    implementDynamicLookupDeleteDataComReqDto.setDynamicLookupDataList(implementDynamicLookupDataComRespDto.getDynamicLookupDataList().stream().map(item -> item.getConEntityId())
                            .collect(Collectors.toList()));/*list-to-strings*///sourceId:1036568_1_45263
                }

                /*D2执行动态寻表删除数据(公共)[7190]   */
                Assert.isNull(implementDynamicLookupDeleteDataComReqDto.getToolTableEncryptionName(), "D2执行私有化分离数据(公共)-D2执行动态寻表删除数据(公共)-设计工具加密表名不能为空", false);
                Assert.isNull(implementDynamicLookupDeleteDataComReqDto.getEntityName(), "D2执行私有化分离数据(公共)-D2执行动态寻表删除数据(公共)-内容表主键字段英文名不能为空", false);
                implementDynamicLookupDeleteDataComRespDto = implementDynamicLookupDeleteDataCom(implementDynamicLookupDeleteDataComReqDto)/*vcase invoke 同服务,同domain*/;


            }
//virtualUsage M2执行通用【循环结束】  45261
            //ModelCode: circulationEnd
        }

//virtualUsage M2修改租户分离数据  45265
        boolean bOOLEAN;
        OmsPlatformOperationSeparatedData omsPlatformOperationSeparatedData = new OmsPlatformOperationSeparatedData();
        omsPlatformOperationSeparatedData.setDataSeparatedStatus("HAVE_COMPLETED");//sourceId:1036581_1_45265
        if (reqDto != null) {
            omsPlatformOperationSeparatedData.setOperationSeparatedDataId(reqDto.getOperationSeparatedDataId());//SimpleFieldAssign//sourceId:1036579_1_45265
        }

        /*M2修改租户分离数据[7206]   */
        Assert.isNull(omsPlatformOperationSeparatedData.getOperationSeparatedDataId(), "D2执行私有化分离数据(公共)-M2修改租户分离数据-分离数据表主键ID不能为空", false);
        Assert.isNull(omsPlatformOperationSeparatedData.getDataSeparatedStatus(), "D2执行私有化分离数据(公共)-M2修改租户分离数据-数据分离状态不能为空", false);
        bOOLEAN = mOmsPlatformOperationSeparatedDataService.updateTenantSeparationData(omsPlatformOperationSeparatedData)/*vcase invoke 本地 method 方法调用;*/;


        ImplementPrivatizationAndSeparationDataComRespDto retData = new ImplementPrivatizationAndSeparationDataComRespDto();
        if (receptionServiceRes_1 != null) {
            retData.setPrivatizationAndSeparationDataList(receptionServiceRes_1.getSpaceList());//list-field-assign//sourceId:1036567_1
        }


        return retData;
    }
    //
}
